using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Singleton<T> : MonoBehaviour where T : Component
{
    private static T instance;

    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<T>();
            }

            if (instance == null)
            {
                GameObject go = new GameObject(typeof(T).Name);
                instance = go.AddComponent<T>();
            }
            
            return instance;
        }
    }
    protected virtual void Awake()
    {
        DontDestroyOnLoad(gameObject); 
        if (instance == null) {
            instance = this as T;
        }
    }
}
